const path = require('path');
const { app } = require('electron');
const fs = require('fs-extra')




exports.mkdir = function (dir) {
    fs.mkdirs(dir, () => { })
}

exports.getUserDataPath = function () {
    return app.getPath("userData")
}

exports.getPluginPath = function () {
    return path.join(exports.getUserDataPath(), 'plugin')
}

exports.getLevelDbPath = function () {
    return path.join(exports.getUserDataPath(), "level_db_data/data")
}

exports.getPluginAsarPathByPluginId = function (pluginId, isFileProtocol = true) {
    const header = isFileProtocol ? "file:///" : ""
    return path.join(header, exports.getPluginPath(pluginId), pluginId + '.asar')
}

exports.getPluginPathByPluginId = function (pluginId, isFileProtocol = false) {
    return path.join(exports.getPluginAsarPathByPluginId(pluginId, isFileProtocol))
}


exports.getPluginInfoPathByPluginId = function (pluginId, isFileProtocol = false) {
    return path.join(exports.getPluginAsarPathByPluginId(pluginId, isFileProtocol), "plugin.json")
}

exports.getSqliteDbPathByName = function (fileName) {
    const thePath = path.join(exports.getUserDataPath(), "sqlite")
    exports.mkdir(thePath)
    return path.join(thePath, fileName + ".db")
}

exports.openFile = function (path) {
    return fs.readFileSync(path, 'utf-8')
}

exports.openJson = function (path) {
    const data = exports.openFile(path)
    try {
        return JSON.parse(data)
    } catch (e) {
        console.error("parse json file error. path=" + path + ",error=" + e)
    }
}